<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

$table_user = C::t('#erling_account_center#er_account_user');

// 操作处理
$operation = $_GET['operation'];
if($operation == 'delete') {
    $id = intval($_GET['id']);
    $user = $table_user->fetch_by_id($id);
    if(empty($user)) {
        cpmsg('user_not_exists', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info', 'error');
    }
    if($table_user->delete_by_id($id)) {
        cpmsg('user_delete_success', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info', 'succeed');
    } else {
        cpmsg('user_delete_failed', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info', 'error');
    }
} elseif($operation == 'edit') {
    $id = intval($_GET['id']);
    $user = $table_user->fetch_by_id($id);
    if(empty($user)) {
        cpmsg('user_not_exists', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info', 'error');
    }
    if(submitcheck('editsubmit')) {
        $status = intval($_POST['status']);
        if($table_user->update($id, array('status' => $status))) {
            cpmsg('user_update_success', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info', 'succeed');
        } else {
            cpmsg('user_update_failed', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info', 'error');
        }
    }
    // 显示编辑表单
    showformheader('plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info&operation=edit&id='.$id);
    showtableheader('编辑用户信息');
    showtablerow('', array('class="vtop"', 'class="vtop"'), array(
        '状态:',
        '<select name="status" class="ps">'
        .'<option value="1"'.($user['status'] == 1 ? ' selected' : '').'>正常</option>'
        .'<option value="2"'.($user['status'] == 2 ? ' selected' : '').'>异常</option>'
        .'</select>'
    ));
    showsubmit('editsubmit', 'submit', '保存');
    showtablefooter();
    showformfooter();
    exit;
}

// 搜索处理
$search = array();
if(!empty($_GET['username'])) {
    $search['username'] = daddslashes(trim($_GET['username']));
}
if(isset($_GET['status']) && $_GET['status'] !== '') {
    $search['status'] = intval($_GET['status']);
}

// 分页设置
$perpage = 20;
$page = max(1, intval($_GET['page']));
$start = ($page - 1) * $perpage;

// 获取用户列表
$userlist = $table_user->fetch_all_with_username($search, $start, $perpage);
$total = $table_user->count_by_search($search);

// 显示表单
showformheader('plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info');
showtableheader('用户绑定记录信息管理');

// 搜索表单
showtablerow('', array('width="80"', 'width="100"', 'width="100"'), array(
    '用户名:', 
    '<input type="text" name="username" value="'.dhtmlspecialchars($_GET['username']).'" class="txt">',
    '状态: <select name="status"><option value="">全部</option><option value="1"'.($_GET['status'] == 1 ? ' selected' : '').'>正常</option><option value="2"'.($_GET['status'] == 2 ? ' selected' : '').'>异常</option></select>',
    '<input type="submit" name="searchsubmit" value="搜索" class="btn">'
));

// 用户列表
showtableheader('', 'nobottom');
showsubtitle(array('ID', '用户名', 'UnionID', '状态', '创建时间', '操作'));

foreach($userlist as $user) {
    showtablerow('', array(), array(
        $user['id'],
        $user['username'],
        $user['erling_unionid'],
        $user['status'] == 1 ? '正常' : '异常',
        dgmdate($user['create_time'], 'Y-m-d H:i'),
        '<a href="'.ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info&operation=edit&id='.$user['id'].'" class="act">编辑</a> '.
        '<a href="javascript:;" onclick="if(confirm(\'确定要删除该用户的绑定记录吗？\')) { window.location.href=\''.ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info&operation=delete&id='.$user['id'].'\'; }" class="act">删除</a>'
    ));
}

// 分页
showtablefooter();
$multi = multi($total, $perpage, $page, 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_account_center&pmod=admin_user_info'.($search ? '&'.http_build_query($search) : ''));
showtablefooter();
showformfooter();